﻿package _7_Text;
/*
 * 演示动态修改文本样式及样式池的使用
 */
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class TJTextPane_Style
{
    public static void main(String args[])throws BadLocationException
    {
        JFrame frm=new JFrame();
        JTextPane jtp=new JTextPane();
        frm.getContentPane().add(new JScrollPane(jtp),BorderLayout.CENTER);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final StyleContext sc=new StyleContext();
        final DefaultStyledDocument dsd=new DefaultStyledDocument(sc);
        jtp.setDocument(dsd);
        jtp.setDragEnabled(true);
        Style defaultStyle=sc.getStyle(StyleContext.DEFAULT_STYLE);

        final Style mainStyle=sc.addStyle("mainStyle", defaultStyle);
        StyleConstants.setForeground(mainStyle, Color.blue);
        StyleConstants.setFontSize(mainStyle, 10);
        StyleConstants.setLeftIndent(mainStyle, 20);
        StyleConstants.setFirstLineIndent(mainStyle, 10);

        final Style secondStyle=sc.addStyle("secondStyle", null);
        StyleConstants.setForeground(secondStyle, Color.red);

        final Style thirdStyle=sc.addStyle("thirdStyle", null);
        StyleConstants.setForeground(thirdStyle, Color.green);
        StyleConstants.setFontSize(thirdStyle, 30);
        StyleConstants.setLeftIndent(thirdStyle, 0);
        
        //简单说，就是设置当前位置增加的段落的默认样式。下一句的样式如果为null，就用这个默认样式
        dsd.setLogicalStyle(0, mainStyle);
        dsd.insertString(0, text, null);
        /*
         *  下面的方法在帮助文档中的参数说明为：
         *  offset - 段落偏移量，该偏移量 >= 0
         *  length - 所影响的字符数，该字符数 >= 0
         *  s - 属性
         *  replace - 确定是替换现有属性还是合并现有属性
         *  从下面的实例使用可以看出，offset并不是段落序号如第1段，第2段，而是段落中的字符在整个文本中的偏移
         *  offset这个偏移量，根据下面的几个设置体会一下吧
         */  
        dsd.setParagraphAttributes(text.indexOf("段2开头"), 1, thirdStyle, false);
        dsd.setParagraphAttributes(text.indexOf("段3开头"), text.indexOf("段4开头")-text.indexOf("段3开头")+1, secondStyle, false);
        //setParagraphAttributes是整段设置，而下面的方法可以只设置段落中的一部分
        dsd.setCharacterAttributes(text.indexOf("段4开头")+4, 2, dsd.getStyle("mainStyle"), false);
        frm.setSize(400,300);
        frm.setVisible(true);
    }
    public static final String text =
          "段1开头Attributes, Styles and Style Contexts段1结尾\n" +
          "段2开头The simple PlainDocument class that you saw in the previous " +
          "chapter is only capable of holding text. The more complex text " +
          "components use a more sophisticated model that implements the " +
          "StyledDocument interface. StyledDocument is a sub-interface of " +
          "Document that contains methods for manipulating attributes that " +
          "control the way in which the text in the document is displayed.段2结尾\n " +
          "段3开头The Swing text package contains a concrete implementation of " +
          "StyledDocument called DefaultStyledDocument that is used as the " +
          "default model for JTextPane and is also the base class from which " +
          "more specific models, such as the HTMLDocument class that handles " +
          "input in HTML format, can be created. In order to make use of " +
          "DefaultStyledDocument and JTextPane, you need to understand how " +
          "Swing represents and uses attributes段3结尾.\n"+
          "段4开头Good night!段4结尾";
}
